package edu.cuit.canary.controller;

import edu.cuit.canary.core.service.BookService;
import edu.cuit.canary.entity.po.Book;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {
    @Resource private BookService service;

    @PostMapping
    public Book save(@RequestBody Book book) {
        return service.save(book);
    }

    @GetMapping("/{id}")
    public Book findById(@PathVariable Integer id) {
        return service.findById(id);
    }

    @DeleteMapping("/{id}")
    public Book removeById(@PathVariable Integer id) {
        Book book = service.findById(id);
        if(book != null) service.removeById(id);
        return book;
    }

    @PutMapping
    public Book updateById(@RequestBody Book book) {
        service.update(book);
        return book;
    }
}
